using System;
using System.ComponentModel;
using System.Windows.Media.Imaging;
using Logic.Units;

namespace GameApplication.Helper
{
    public class UnitWpf : INotifyPropertyChanged, INotifyPropertyChanging
    {
        public readonly IUnit Unit;
        private int _count;

        public UnitWpf(IUnit unit)
        {
            Unit = unit;
            MaxHp = unit.Count*unit.HP;
            _count = Unit.Count;

            Unit.ChangeCount += () => { Count = Unit.Count; };

            Unit.ChangeActualHp += () => { ActualHp = Unit.ActualHp; };
        }

        public int MaxHp { get; private set; }

        public int Count
        {
            get { return Unit.Count; }
            set
            {
                if (PropertyChanging != null)
                {
                    PropertyChanging(this, new PropertyChangingEventArgs("Count"));
                }
                Unit.Count = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Count"));
                }
            }
        }

        public int LocalCount
        {
            get { return _count; }
            set
            {
                if (PropertyChanging != null)
                {
                    PropertyChanging(this, new PropertyChangingEventArgs("LocalCount"));
                }
                _count = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("LocalCount"));
                }
            }
        }

        public int ActualHp
        {
            get { return Unit.ActualHp; }
            set
            {
                if (PropertyChanging != null)
                {
                    PropertyChanging(this, new PropertyChangingEventArgs("ActualHp"));
                }
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("ActualHp"));
                }
            }
        }

        public BitmapImage ImageUri
        {
            get
            {
                string state = Unit.State == UserState.Player ? "P" : "E";
                int id = 0;
                switch (Unit.GetType().Name)
                {
                    case "Infantry":
                        id = 1;
                        break;
                    case "ArmoredCar":
                        id = 2;
                        break;
                    case "Tank":
                        id = 3;
                        break;
                }
                return
                    new BitmapImage(new Uri(String.Format("pack://application:,,,/Res/{0}_{1}.png", id, state),
                        UriKind.Absolute));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public event PropertyChangingEventHandler PropertyChanging;
    }
}